<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Scrollpic extends CI_Controller {

	private $tab = 'site';

	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /ychadmin/login');
			exit;
		}
	}

	function add_view(){
		$this->load->helper(array('form'));

		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'scrollpic_add';

		$this->load->view("/ychadmin/scrollpic_add",$vdata);
	}

	function add(){
		$url 		= $this->input->post('url');
		$title 		= $this->input->post('title');
		$forder 	= $this->input->post('forder');
		//如果有上传图片，则先完成图片上传
		if(!file_exists(UPFILES_SCROLLPIC_IMG)){
			mkdir(UPFILES_SCROLLPIC_IMG,0777);
		}
		$pic = "";
		if($_FILES['pic']['name'] != '')
		{
			$pic = sha1($_SERVER['HTTP_USER_AGENT'].$_SERVER['REMOTE_ADDR'].time().rand());
			$tmpfn = $_FILES['pic']['name'];
			$pt=strrpos($tmpfn, ".");
			$retval=substr($tmpfn, $pt+1, strlen($tmpfn) - $pt);
			$pic.=".".$retval;
			$upfile = UPFILES_SCROLLPIC_IMG."/".$pic;
			if(is_uploaded_file($_FILES['pic']['tmp_name']))
                if(move_uploaded_file($_FILES['pic']['tmp_name'],$upfile));

		}

		$this->load->helper(array('form'));
		$this->load->library('form_validation');
		$this->form_validation->set_rules('url', 				'URL', 		'trim|min_length[1]|max_length[100]');
		$this->form_validation->set_rules('title', 				'标题', 		'required|trim|min_length[1]|max_length[50]');
		$this->form_validation->set_rules('forder', 			'排序', 		'trim|integer|min_length[1]|max_length[20]');
		if ($this->form_validation->run() == FALSE){
   			$this->load->view('/ychadmin/scrollpic_add');
   			return;
  		}

  		$data['url'] 		= $url;
  		$data['title'] 		= $title;
  		$data['forder'] 	= $forder;
  		$data['pic'] 		= $pic;
  		$data['add_time'] 	= time();
  		$data['addip'] 		= $this->js->get_ip();
  		$this->load->model('scrollpic_model');
  		$scrollpic_id = $this->scrollpic_model->add($data);
        $this->load->model('log_model');
        $this->log_model->add_log('[添加]滚动图片]['.$title.']');
  		header("location:/ychadmin/scrollpic/scrollpiclist");
   		exit;
	}

	function view($scrollpic_id){
		$this->load->model('scrollpic_model');
  		$scrollpic = $this->scrollpic_model->get_scrollpic_obj($scrollpic_id);
  		$vdata['scrollpic'] = $scrollpic;

  		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'scrollpic';

  		$this->load->view("/ychadmin/scrollpic_view", $vdata);
	}

	function edit_view($scrollpic_id){
		$this->load->helper(array('form'));
		$this->load->model('scrollpic_model');
  		$scrollpic = $this->scrollpic_model->get_scrollpic_obj($scrollpic_id);
  		$vdata['scrollpic'] = $scrollpic;

  		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'scrollpic';

  		$this->load->view("/ychadmin/scrollpic_edit", $vdata);
	}

	function edit(){
		$id 		= $this->input->post('id');
		$url 		= $this->input->post('url');
		$title 		= $this->input->post('title');
		$forder 	= $this->input->post('forder');

		$bpic 		= $this->input->post('bpic');
		$pic = "";
		if($_FILES['pic']['name'] != ''){
			$pic = sha1($_SERVER['HTTP_USER_AGENT'].$_SERVER['REMOTE_ADDR'].time().rand());
			$tmpfn = $_FILES['pic']['name'];
			$pt=strrpos($tmpfn, ".");
			$retval=substr($tmpfn, $pt+1, strlen($tmpfn) - $pt);
			$pic.=".".$retval;
			$upfile = trim(UPFILES_SCROLLPIC_IMG, '/')."/".$pic;
			if(is_uploaded_file($_FILES['pic']['tmp_name']))
				move_uploaded_file($_FILES['pic']['tmp_name'],$upfile);
		}
		$pic = $pic?$pic:$bpic;

		$this->load->helper(array('form'));
		$this->load->library('form_validation');
		$this->form_validation->set_rules('id', 				'ID', 		'required');
		$this->form_validation->set_rules('url', 				'URL', 		'trim|min_length[1]|max_length[100]');
		$this->form_validation->set_rules('title', 				'标题', 		'required|trim|min_length[1]|max_length[50]');
		$this->form_validation->set_rules('forder', 			'排序', 		'trim|integer|min_length[1]|max_length[20]');
		if ($this->form_validation->run() == FALSE){
   			$this->load->view('/ychadmin/scrollpic_add');
   			return;
  		}

  		$data['url'] 		= $url;
  		$data['title'] 		= $title;
  		$data['forder'] 	= $forder;
        if($pic){
            $data['pic'] 		= $pic;
        }
  		$this->load->model('scrollpic_model');
  		$scrollpic_id = $this->scrollpic_model->update($id, $data);

  		//header("location:/ychadmin/scrollpic/scrollpiclist");

  		$this->js->go('/ychadmin/scrollpic/scrollpiclist.html');

  		$this->load->model('log_model');
  		$this->log_model->add_log('[修改][滚动图片]'.'ID=='.$id);
   		exit;
	}

	function scrollpiclist(){
		$this->load->model('scrollpic_model');
		$vdata = $this->scrollpic_model->get_scrollpic_list();

		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'scrollpic';

		$this->load->view('/ychadmin/scrollpic_list', $vdata);
	}

	function remove(){
		$id 				= $this->input->post('id');
  		$data["is_del"]		= 1;
  		$this->load->model('scrollpic_model');
		echo  $this->scrollpic_model->update($id, $data);
		$this->load->model('log_model');$this->log_model->add_log('[删除][滚动图片]');
	}

}
